<!DOCTYPE HTML>
<script src="../resources/gc.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>

<!--

Accessibility Object Model
Explainer: https://github.com/WICG/aom/blob/gh-pages/explainer.md
Spec: https://wicg.github.io/aom/spec/

-->

<script>
test(function(t) {
    assert_true(internals.runtimeFlags.accessibilityObjectModelEnabled);
}, "Make sure that Accessibility Object Model is enabled");
</script>

<div id="container"></div>

<script>
test(function(t) {
    assert_throws_dom('InvalidAccessError', function() {
        var container = document.getElementById("container");
        container.accessibleNode.appendChild(document.body.accessibleNode);
    });
}, "Can't call AccessibleNode.appendChild on an AccessibleNode with an Element");
</script>

<script>
test(function(t) {
    var container = document.getElementById("container");
    var child = new AccessibleNode();
    child.role = "button";
    child.label = "Launch";
    container.accessibleNode.appendChild(child);

    var axContainer = accessibilityController.accessibleElementById("container");
    assert_equals(axContainer.childrenCount, 1);
    var axChild = axContainer.childAtIndex(0);
    assert_equals(axChild.role, "AXRole: AXButton");
    assert_equals(axChild.name, "Launch");
}, "Create a virtual AccessibleNode");
</script>

<div id="container2"></div>

<script>
test(function(t) {
    var container = document.getElementById("container2");

    var list = new AccessibleNode();
    list.role = "list";
    container.accessibleNode.appendChild(list);

    var item1 = new AccessibleNode();
    item1.role = "listitem";
    list.appendChild(item1);

    var item2 = new AccessibleNode();
    item2.role = "listitem";
    list.appendChild(item2);

    var axContainer = accessibilityController.accessibleElementById("container2");
    assert_equals(axContainer.childrenCount, 1);
    var axList = axContainer.childAtIndex(0);
    assert_equals(axList.role, "AXRole: AXList");

    assert_equals(axList.childrenCount, 2);
    var axListItem = axList.childAtIndex(0);
    assert_equals(axListItem.role, "AXRole: AXListItem");
    var axListItem2 = axList.childAtIndex(1);
    assert_equals(axListItem2.role, "AXRole: AXListItem");
}, "Create a virtual list using AccessibleNodes");
</script>

<script>
test(function(t) {
    var container = document.getElementById("container2");

    var list = new AccessibleNode();
    list.role = "list";
    container.accessibleNode.appendChild(list);

    var item1 = new AccessibleNode();
    item1.role = "listitem";
    list.appendChild(item1);

    var item2 = new AccessibleNode();
    item2.role = "listitem";
    list.appendChild(item2);

    var item3 = new AccessibleNode();
    item3.role = "listitem";
    list.appendChild(item3);

    // Check the list was setup correctly.
    assert_equals(list.childNodes.length, 3);
    assert_equals(list.childNodes[0], item1);
    assert_equals(list.childNodes[1], item2);
    assert_equals(list.childNodes[2], item3);

    // Remove middle node.
    list.removeChild(list.childNodes[1]);
    assert_equals(list.childNodes.length, 2);
    assert_equals(list.childNodes[0], item1);
    assert_equals(list.childNodes[1], item3);
}, "Remove a node from a container");
</script>

<script>
test(function(t) {
    var container = document.getElementById("container2");

    var list1 = new AccessibleNode();
    list1.role = "list";
    container.accessibleNode.appendChild(list1);

    var list2 = new AccessibleNode();
    list2.role = "list";
    container.accessibleNode.appendChild(list2);

    var item = new AccessibleNode();
    item.role = "listitem";
    list1.appendChild(item);

    // TODO: support reparenting and then fix this test.
    assert_throws_dom('NotSupportedError', function() {
      list2.appendChild(item);
    });
}, "Reparenting is not supported yet");
</script>
